package com.hmall.search.controller;

import com.hmall.common.clients.ItemClient;
import com.hmall.common.dto.ItemPageQueryDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.search.pojo.ItemDoc;
import com.hmall.search.service.SearchService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private SearchService searchService;

    @Autowired
    private ItemClient itemClient;
    /**
     * 测试微服务之间调用是否拦截
     * 地址:http://localhost:10010/search/test
     * @return {@link String}
     */
    @GetMapping("/test")
    public PageDTO<Item> testFeign(){
        PageDTO<Item> list = itemClient.list(1, 10);
        System.out.println(list);
        return list;
    }


    /**
     * 基本搜索功能
     *
     * @param itemPageQueryDTO
     * @return {@link PageDTO}<{@link Item}>
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> list(@RequestBody ItemPageQueryDTO itemPageQueryDTO){
        return searchService.list(itemPageQueryDTO);
    }

    /**
     * 过滤项聚合功能 --- 搜手机只会出现手机对应的品牌和其价格
     *
     * @return {@link Map}<{@link String}, {@link List}<{@link String}>>
     */
    @PostMapping("/filters")
    public Map<String, List<String>> filters(@RequestBody ItemPageQueryDTO itemPageQueryDTO){
        return searchService.filters(itemPageQueryDTO);
    }

    /**
     * 搜索栏自动补全功能
     *
     * @param key
     * @return {@link List}<{@link String}>
     */
    @GetMapping("/suggestion")
    public List<String> suggestion(String key){
        return searchService.suggestion(key);
    }

}
